import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

const api = {
  hello: 'world',
  setTitle: (title) => {
    ipcRenderer.send('set-title', title)
  },

  onece: (chennel, callback) => {
    ipcRenderer.once(chennel, callback)
  },

  on: (chennel, callback) => {
    ipcRenderer.on(chennel, callback)
  },

  send: (chennel, data) => {
    ipcRenderer.send(chennel, data)
  },

  invoke: async (chennel, data) => {
    return await ipcRenderer.invoke(chennel, data)
  }
}

if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}
